package cn.hl.org.service.impl;

import cn.hl.basic.service.impl.BaseServiceImpl;
import cn.hl.basic.util.Md5Utils;
import cn.hl.basic.util.StrUtils;
import cn.hl.org.domain.Employee;
import cn.hl.org.mapper.EmployeeMapper;
import cn.hl.org.service.IEmployeeService;
import cn.hl.user.domain.Logininfo;
import cn.hl.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void add(Employee employee) {
        // 获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        // 添加logininfo
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.add(logininfo);
        // 添加employee
        employeeMapper.add(employee);
        // 添加中间表t_employee_role
        employeeMapper.addEmployeeRole(employee);
    }

    @Override
    public void delete(Long id) {
        // 获取logininfo的id
        Long logininfoId = employeeMapper.findById(id).getLogininfo_id();
        // 删除logininfo
        logininfoMapper.delete(id);
        // 删除中间表t_employee_role
        employeeMapper.deleteEmployeeRoleByEmployeeId(id);
        // 删除employee
        employeeMapper.delete(id);
    }

    @Override
    public void update(Employee employee) {
        // 获取随机盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 获取加密之后的密码
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        // 获取logininfo对象的id
        Long logininfo_id = employeeMapper.findById(employee.getId()).getLogininfo_id();
        Logininfo logininfo = employee2Logininfo(employee);
        logininfo.setId(logininfo_id);
        // 修改logininfo
        logininfoMapper.update(logininfo);
        // 修改employee
        employeeMapper.update(employee);
        // 删除中间表t_employee_role
        employeeMapper.deleteEmployeeRoleByEmployeeId(employee.getId());
        // 添加中间表t_employee_role
        employeeMapper.addEmployeeRole(employee);
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        // 使用工具类深度拷贝
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        return logininfo;
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public List<Employee> findByShopId(Long id) {
        return employeeMapper.findByShopId(id);
    }
}
